

//老福特随机原神帖子插件
//作者: 水脚脚
//使用前,请先安装 cheerio包,
//在云崽根目录输入:
//pnpm add cheerio -w

import plugin from '../../lib/plugins/plugin.js'
import { segment } from "oicq"
import puppeteer from 'puppeteer'
import cheerio from 'cheerio'

export class lofter_gs extends plugin {
    constructor() {
        super({
            /** 功能名称 */
            name: '老福特热门',
            /** 功能描述 */
            dsc: '老福特热门',
            /** https://oicqjs.github.io/oicq/#events */
            event: 'message',
            /** 优先级，数字越小等级越高 */
            priority: 1000,
            rule: [
                {
                    reg: '^#*老福特$',
                    fnc: 'get_article'
                },

            ]
        })
    }

    async get_article(e) {
        let html = '';
        // e.reply(segment.image('https://imglf6.lf127.net/img/c181ddfcd781003d/YWpEYWpIdTlEZWNrTk03Vnp1QVoveGsvQVlBS3B1ZkVZWkZ6MnRBS2xTVT0.png?imageView&thumbnail=500x0&quality=96&stripmeta=0'))
        // let lofetr_url = 'https://www.lofter.com/tag/'+`${tag}`;
        let page = Math.floor(Math.random() * 10)+1;//前十页随机一页
        let lofetr_url = `https://www.lofter.com/tag/%E5%8E%9F%E7%A5%9E?page=`+String(page);
        html = await gethtml(lofetr_url);
        let article_data = await parse(html)
        // console.log("article_data:", article_data);
        let msg = [];
        let i = Math.floor(Math.random() * article_data.length);//随机发一条消息
        if (article_data[i].img != undefined) {
            msg.push(segment.image(article_data[i].img));
        }
        msg.push(article_data[i].text)
        // console.log("article_data[i].img:", article_data[i].img);
        e.reply(msg)
        // await ForwardMsg(e, msg);
        return;
    }
}

//发送转发消息
//输入data一个数组,元素是字符串,每一个元素都是一条消息.
async function ForwardMsg(e, data) {
    // console.log(data);
    let msgList = [];
    for (let i of data) {
        msgList.push({
            message: i,
            // nickname: Bot.nickname,
            nickname: '不愿意透露姓名的热心市民',
            user_id: e.user_id,
            // user_id: Bot.uin,
        });
    }
    if (msgList.length == 1) {
        await e.reply(segment.text(msgList[0].message));
    }
    else {
        let msg = await Bot.makeForwardMsg(msgList);
        console.log(msg);
        await e.reply(msg);
    }
    return;
}



async function gethtml(url) {
    const browser = await puppeteer.launch({
        args: ['--no-sandbox']
    })
    const page = await browser.newPage()
    await page.goto(url)
    const html = await page.content()
    // const results = parse(html)

    await browser.close()
    return html
}

async function parse(html) {
    const $ = cheerio.load(html)
    let results = []
    $('div.m-icnt.ctag').each((i, link) => {
        let obj_data = {
            text: '',
            img: ''
        }
        obj_data.img = $(link).find('img').attr('src');
        obj_data.text = $(link).find('p').text();
        results[i] = obj_data;
    })
    // console.log("res", results)
    return results
}
